// ignore_for_file: file_names, deprecated_member_use

import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Utils {
  static List<String>? _words;

  static readAssetsJsonFile(String path) async {
    final content = await rootBundle.loadString(path);
    return jsonDecode(content);
  }

  static readAssetsStringFile(String path) async {
    return await rootBundle.loadString(path);
  }

  static Future<String?> randomWord(String? word) async {
    if (_words == null) {
      String? str = await readAssetsStringFile("assets/misc/words.txt");
      _words = str?.split('\n');
    }

    if (_words != null) {
      int randomIndex = Random().nextInt(_words!.length);
      String? randomWord = _words?.elementAt(randomIndex);
      if (word != null && randomWord == word) {
        randomIndex = (randomIndex + 1) % _words!.length;
        randomWord = _words?.elementAt(randomIndex);
      }

      if (randomWord == word) return null;
      return randomWord;
    }

    return null;
  }

  static Future<dynamic> getJson(String url) async {
    var httpClient = HttpClient();

    try {
      var request = await httpClient.getUrl(Uri.parse(url));
      var response = await request.close();
      if (response.statusCode == HttpStatus.OK) {
        var json = await response.transform(utf8.decoder).join();
        return jsonDecode(json);
      }
    } catch (e) {
      return null;
    }
  }

  static MaterialColor createMaterialColor(Color color) {
    List strengths = <double>[.05];
    Map<int, Color> swatch = <int, Color>{};
    final int r = color.red, g = color.green, b = color.blue;

    for (int i = 1; i < 10; i++) {
      strengths.add(0.1 * i);
    }
    for (var strength in strengths) {
      final double ds = 0.5 - strength;
      swatch[(strength * 1000).round()] = Color.fromRGBO(
        r + ((ds < 0 ? r : (255 - r)) * ds).round(),
        g + ((ds < 0 ? g : (255 - g)) * ds).round(),
        b + ((ds < 0 ? b : (255 - b)) * ds).round(),
        1,
      );
    }
    return MaterialColor(color.value, swatch);
  }

  static String seconds2str(int seconds) {
    var s = seconds % 60;
    var m = seconds ~/ 60;
    var ss = s < 10 ? '0$s' : '$s';
    var ms = m < 10 ? '0$m' : '$m';
    return '$ms:$ss';
  }

  static appendUrl(String url1, String url2) {
    if (url1.endsWith('/')) {
      if (url2.startsWith('/')) {
        return '$url1${url2.substring(1)}';
      }
      return '$url1$url2';
    }

    if (url2.startsWith('/')) {
      return '$url1$url2';
    }
    return '$url1/$url2';
  }

  static getAudioName(String url) {
    var pos1 = url.indexOf(".");
    if (pos1 < 0) {
      pos1 = 0;
    } else {
      pos1 = pos1 + 1;
    }
    var pos2 = url.lastIndexOf(".");
    if (pos2 < 0) pos2 = url.length;
    return url.substring(pos1, pos2);
  }
}
